package com.atguigu.gmall.canal.config;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.model.cart.CartInfo;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;

/**
 * @author chen.qian
 * @date 2018/3/19
 */
@CanalEventListener
public class MyEventListener {
    @Autowired
    @Qualifier("redis")
    private RedisTemplate redisTemplate;

//    @InsertListenPoint
//    public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
//        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
//    }
//
//    @UpdateListenPoint
//    public void onEvent1(CanalEntry.RowData rowData) {
//        System.err.println("UpdateListenPoint");
//        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
//    }
//
//    @DeleteListenPoint
//    public void onEvent3(CanalEntry.EventType eventType) {
//        System.err.println("DeleteListenPoint");
//    }

    @ListenPoint(destination = "example", schema = "gmall_order", table = {"cart_info"},
            eventType = {CanalEntry.EventType.UPDATE, CanalEntry.EventType.DELETE, CanalEntry.EventType.INSERT})
    public void onEvent4(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        CartInfo cartInfo = new CartInfo();

        rowData.getAfterColumnsList().forEach((c)->{
            System.out.println(c.getName()+"::"+c.getValue());
                switch (c.getName()){
                    case "user_id":
                        cartInfo.setUserId(c.getValue());
                        break;
                    case "sku_id":
                        cartInfo.setSkuId(Long.parseLong(c.getValue()));
                        break;
                    case "cart_price":
                        cartInfo.setCartPrice(new BigDecimal(c.getValue()));
                        break;
                    case  "sku_num"  :
                        cartInfo.setSkuNum(Integer.parseInt(c.getValue()));
                        break;
                    case "img_url":
                        cartInfo.setImgUrl(c.getValue());
                        break;
                    case "sku_name":
                        cartInfo.setSkuName(c.getValue());
                        break;
                    case "is_checked":
                        cartInfo.setIsChecked(Integer.parseInt(c.getValue()));
                        break;
                    case "id":
                        cartInfo.setId(Long.parseLong(c.getValue()));
                        break;
                }
        });
        if(eventType.compareTo(CanalEntry.EventType.DELETE)==0){
           redisTemplate.opsForHash().delete(RedisConst.USER_KEY_PREFIX+
                   cartInfo.getUserId()+RedisConst.USER_CART_KEY_SUFFIX, cartInfo.getSkuId().toString());
        }
        if(eventType.compareTo(CanalEntry.EventType.INSERT)==0||eventType.compareTo(CanalEntry.EventType.UPDATE)==0){
            redisTemplate.opsForHash().put(
                    RedisConst.USER_KEY_PREFIX+
                            cartInfo.getUserId()+RedisConst.USER_CART_KEY_SUFFIX,
                    cartInfo.getSkuId().toString(),cartInfo);
        }
    }
}
